package com.ktjiaoyu.news.service.impl;

import com.ktjiaoyu.news.dao.UserDao;
import com.ktjiaoyu.news.dao.impl.UserDaoImpl;
import com.ktjiaoyu.news.entity.User;
import com.ktjiaoyu.news.service.UserServiceDao;
import com.ktjiaoyu.news.utils.Page;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserServiceDaoImpl implements UserServiceDao {
    private UserDao userDao=new UserDaoImpl();
    @Override
    public Page<User> getPage(User user, Integer pageNo, Integer pageSize) {
        Page<User> page=new Page<>();
        //获得总数
        int count=userDao.count();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        //设置总数量，计算总页数
        page.setTotalCount(count);
        List<User> list=userDao.getPage(user,pageNo,pageSize);
        page.setData(list);
        return page;
    }


    @Override
    public List<User> getAll() {
        return Collections.emptyList();
    }

    @Override
    public User getByIdUser(Integer id) {
        return userDao.getByIdUser(id);
    }

    @Override
    public int insertUser(User user) {
        return 0;
    }

    @Override
    public boolean deleteUser(Integer id) {
        return userDao.deleteUser(id)>0;
    }

    @Override
    public boolean update(User user) {
        if(user.getId() != null){
            // 如果有ID就是修改
            return userDao.update(user) > 0;
        }
        // 没有id就是新增
        return userDao.insertUser(user) > 0;
    }
}
